package com.minshenglife.zanzan.web.weixin;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.minshenglife.zanzan.entity.weixin.AutomaticReply;
import com.minshenglife.zanzan.entity.weixin.Keyword;
import com.minshenglife.zanzan.entity.weixin.Material;
import com.minshenglife.zanzan.pub.utils.Message;
import com.minshenglife.zanzan.service.ArticleService;
import com.minshenglife.zanzan.service.AutomaticReplyService;
import com.minshenglife.zanzan.service.KeywordService;
import com.minshenglife.zanzan.service.MaterialService;
import com.minshenglife.zanzan.web.weixin.model.KeywordModel;
import com.nationsky.pub.utils.GsonUtils;

/**
 * 关键词自动回复controller
 * @author jianghaibo
 *
 */
@Controller
@RequestMapping("/autoReply")
public class EMOAutomaticReplyController {

	@Resource
	AutomaticReplyService automaticReplyService;
	@Resource
	KeywordService keywordService;
	@Resource
	MaterialService materialService;
	@Resource
	ArticleService articleService;
	
	@RequestMapping(value="manage.mge",method=RequestMethod.GET)
    public String findAll(String appid,ModelMap map,HttpServletRequest request, HttpServletResponse response){
		try {
			List<AutomaticReply> automaticReplieList = automaticReplyService.findAllByAppid(appid);
			System.out.println("关键词列表--》"+GsonUtils.toGson(automaticReplieList));
			map.put("list", automaticReplieList);
			map.put("appid", appid);
			Material material = new Material();
			material.setAppid(appid);
			//图文列表
			material.setMaterialType("news");
			List<Material> materials = materialService.findAll(material);
			map.put("materials", materials);
			//图片列表
			material.setMaterialType("image");
			List<Material> imageMmaterials = materialService.findAll(material);
			map.put("imageMaterials", imageMmaterials);
			//音频列表
			material.setMaterialType("voice");
			List<Material> voiceMmaterials = materialService.findAll(material);
			map.put("voiceMaterials", voiceMmaterials);
			//视频列表
			material.setMaterialType("video");
			List<Material> videoMmaterials = materialService.findAll(material);
			map.put("videoMaterials", videoMmaterials);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/jsp/manage/wxAutomaticReplyManage";
	}
	
	@RequestMapping(value="add.mge",method=RequestMethod.POST)
	@ResponseBody
    public Message add(AutomaticReply automaticReply,KeywordModel keywordModel,HttpServletRequest request, HttpServletResponse response){
		Message message=null;
		System.out.println("关键词列表为--->"+ GsonUtils.toGson(keywordModel));
		System.out.println("自动回复规则为--->"+ GsonUtils.toGson(automaticReply));
		try {
			Material material = automaticReply.getMaterial();
			if(material != null && material.getMaterialType() != null && material.getMaterialType().equals("text")){
				material.setAppid(automaticReply.getAppid());
				material = materialService.add(material);
			}
			automaticReply.setMaterialId(material.getId());
			automaticReply = automaticReplyService.add(automaticReply);
			List<Keyword> keywordList = keywordModel.getKeywords();
			for (Keyword keyword : keywordList) {
				if(keyword != null && keyword.getKeyWord() != null){
					keyword.setAppid(automaticReply.getAppid());
					keyword.setAutomaticId(automaticReply.getId());
					keywordService.add(keyword);
				}
			}
			message = new Message(Message.STATE_200,"保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			message = new Message(Message.STATE_one,"服务器异常");
		}
		return message;
	}
	
	@RequestMapping(value="delete.mge",method=RequestMethod.DELETE)
	@ResponseBody
    public Message delete(AutomaticReply automaticReply, HttpServletRequest request, HttpServletResponse response){
		Message message=null;
		boolean isSuccessFordeleteKeyword = false;
		boolean isSuccessForDeleteAuto = false;
		System.out.println("要删除的自动回复规则为--》"+GsonUtils.toGson(automaticReply));
		Long materialId = null;
		try {
			Keyword keyword = new Keyword();
			keyword.setAutomaticId(automaticReply.getId());
			isSuccessFordeleteKeyword = keywordService.delete(keyword);
			if(isSuccessFordeleteKeyword){
				automaticReply = automaticReplyService.get(automaticReply.getId());
				isSuccessForDeleteAuto = automaticReplyService.delete(automaticReply);
			}
			if(isSuccessForDeleteAuto){
				Material material = materialService.get(automaticReply.getMaterialId());
				if(material != null && material.getMaterialType().equals("text")){
					materialService.delete(material);
				}
				message = new Message(Message.STATE_200,"删除成功");
			}else{
				message = new Message(Message.STATE_one,"删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			message = new Message(Message.STATE_one,"服务器异常");
		}
		return message;
	}
	
	@RequestMapping(value="update.mge",method=RequestMethod.POST)
	@ResponseBody
    public Message update(AutomaticReply automaticReply,KeywordModel keywordModel,HttpServletRequest request, HttpServletResponse response){
		Message message=null;
		try {
			Material material = automaticReply.getMaterial();
			System.out.println("自动回复的修改为--》"+GsonUtils.toGson(automaticReply));
			System.out.println("修改的素材为--》"+GsonUtils.toGson(material));
			if(material != null && material.getMaterialType() != null && material.getMaterialType().equals("text")){
				materialService.update(material);
			}
			automaticReply.setMaterialId(material.getId());
			automaticReplyService.update(automaticReply);
			Keyword tempKeyword = new Keyword();
			tempKeyword.setAutomaticId(automaticReply.getId());
			keywordService.delete(tempKeyword);
			List<Keyword> keywordList = keywordModel.getKeywords();
			for (Keyword keyword : keywordList) {
				if(keyword != null && keyword.getKeyWord() != null){
					keyword.setAppid(automaticReply.getAppid());
					keyword.setAutomaticId(automaticReply.getId());
					keywordService.add(keyword);
				}
			}
			message = new Message(Message.STATE_200,"保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			message = new Message(Message.STATE_one,"服务器异常");
		}
		return message;
	}
	
	
}
